﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct TeacherSecond
{
	char* name;
	char** students;
};
int allocateSecondSpace(struct TeacherSecond*** temp)
{
	//1.三级指针temp就是为了修改二级指针的地址；
	if (NULL == temp)
	{
		//2.错误码，不同错误码表示不同错误；
		return -1;
	}

	struct TeacherSecond** ts = malloc(sizeof(struct TeacherSecond*) * 3);
	for (int i = 0; i < 3; ++i)
	{
		//3.给struct TeacherSecond*结构体指针分配空间；
		ts[i] = malloc(sizeof(struct TeacherSecond));

		//4.给struct TeacherSecond*结构体指针，指针char* name分配空间；
		ts[i]->name = malloc(sizeof(char) * 64);
		memset(ts[i]->name, 0, 64);
		sprintf(ts[i]->name, "Teacher_%d", i + 1);

		//5.给struct TeacherSecond*结构体指针，二级指针char** students分配空间；
		ts[i]->students = malloc(sizeof(char*) * 4);
		for (int j = 0; j < 4; ++j)
		{
			ts[i]->students[j] = malloc(sizeof(char) * 64);
			memset(ts[i]->students[j], 0, 64);
			sprintf(ts[i]->students[j], "%s_Stu_%d", ts[i]->name, j + 1);
		}
	}
	*temp = ts;
	return 0;
}
//int printTeachers(struct TeacherSecond** teachers)
void printTeachers(struct TeacherSecond** teachers)
{
	/*if (NULL == teachers)
	{
		return -1;
	}*/
	if (NULL == teachers)
	{
		return;
	}
	for (int i = 0; i < 3; ++i)
	{
		printf("6.输出teachers[i]的name：%s\n", teachers[i]->name);
		for (int j = 0; j < 4; ++j)
		{
			printf("7.输出teachers[i]对应的students[j]的name：%s\n", teachers[i]->students[j]);
		}
	}
}
void freeSecondSpace(struct TeacherSecond** teachers)
{
	if (NULL == teachers)
	{
		return;
	}
	for (int i = 0; i < 3; ++i)
	{
		if (teachers[i] == NULL)
		{
			continue;
		}

		if (teachers[i]->name != NULL)
		{
			free(teachers[i]->name);
			teachers[i]->name = NULL;
		}
		for (int j = 0; j < 4; ++j)
		{
			if (teachers[i]->students[j] != NULL)
			{
				free(teachers[i]->students[j]);
				teachers[i]->students[j] = NULL;
			}
		}
		free(teachers[i]->students);
		teachers[i]->students = NULL;


		free(teachers[i]);
		teachers[i] = NULL;

	}
	free(teachers);
	teachers = NULL;
}
void testSecondPointer()
{
	struct TeacherSecond** teachers = NULL;
	int ret = 0;
	ret = allocateSecondSpace(&teachers);
	if (ret < 0)
	{
		printf("8.调用allocateSecondSpace()函数出错！\n");
		return;
	}

	//9.打印输出老师及学生信息；
	printTeachers(teachers);
	/*
		6.输出teachers[i]的name：Teacher_1
		7.输出teachers[i]对应的students[j]的name：Teacher_1_Stu_1
		7.输出teachers[i]对应的students[j]的name：Teacher_1_Stu_2
		7.输出teachers[i]对应的students[j]的name：Teacher_1_Stu_3
		7.输出teachers[i]对应的students[j]的name：Teacher_1_Stu_4
		6.输出teachers[i]的name：Teacher_2
		7.输出teachers[i]对应的students[j]的name：Teacher_2_Stu_1
		7.输出teachers[i]对应的students[j]的name：Teacher_2_Stu_2
		7.输出teachers[i]对应的students[j]的name：Teacher_2_Stu_3
		7.输出teachers[i]对应的students[j]的name：Teacher_2_Stu_4
		6.输出teachers[i]的name：Teacher_3
		7.输出teachers[i]对应的students[j]的name：Teacher_3_Stu_1
		7.输出teachers[i]对应的students[j]的name：Teacher_3_Stu_2
		7.输出teachers[i]对应的students[j]的name：Teacher_3_Stu_3
		7.输出teachers[i]对应的students[j]的name：Teacher_3_Stu_4
	*/

	//10.释放内存；
	freeSecondSpace(teachers);
	teachers = NULL;
}

//main209：结构体嵌套二级指针
int main209()
{
	testSecondPointer();

	system("pause");
	return EXIT_SUCCESS;
}